<!-- 广告编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'编辑标签模板':'添加标签模板'"
    :visible="visible"
    width="700px"
    :destroy-on-close="true"
    :lock-scroll="false"
    :before-close="close"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">
      <el-form-item label="标签用途" prop="tag_type">
        <template>
          <el-radio v-model="form.tag_type" :label=1>订单与客户</el-radio>
          <el-radio v-model="form.tag_type" :label=2>通用</el-radio>
        </template>
      </el-form-item>
      <el-form-item label="标签名称:" prop="tag_name">
        <el-input
          clearable
          :maxlength="56"
          v-model="form.tag_name"
          show-word-limit
          @input="editTagLab"
          placeholder="请输入模板名称"/>
      </el-form-item>
      <el-form-item label="标签样式:" prop="tag_color">
        <el-radio v-model="form.tag_color" label='default'><el-tag label='default'>{{form.tag_name_lab}}</el-tag></el-radio>
        <el-radio v-model="form.tag_color" label='success'><el-tag type="success">{{form.tag_name_lab}}</el-tag></el-radio>
        <el-radio v-model="form.tag_color" label='info'><el-tag type="info">{{form.tag_name_lab}}</el-tag></el-radio>
        <el-radio v-model="form.tag_color" label='warning'><el-tag type="warning">{{form.tag_name_lab}}</el-tag></el-radio>
        <el-radio v-model="form.tag_color" label='danger'><el-tag type="danger">{{form.tag_name_lab}}</el-tag></el-radio>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="$emit('close')" >取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

export default {
  name: 'AdEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
  },
  components: {},
  data() {
    return {
      // 表单数据
      form: Object.assign({tag_type: 1,tag_name_lab:'标签'}, this.data),
      // 表单验证规则
      rules: {
        tag_color: [
          {required: true, message: '请选择标签样式', trigger: 'blur'}
        ],
        tag_type: [
          {required: true, message: '请选择标签用途', trigger: 'blur'}
        ],
        tag_name: [
          {required: true, message: '请输入标签名称', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      blurIndex: null,//记录光标位置
      blurIndexContent: null,//记录光标位置
      templateTypeList:[],//模板类型
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
        this.editTagLab();
      } else {
        this.form = {
          tag_name_lab:'标签'
        };
        this.isUpdate = false;
      }
    }
  },
  computed: {
  },
  methods: {
    /* 修改标签内容 */
    editTagLab()
    {
      this.form.tag_name_lab=this.form.tag_name;
    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/tagManage/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /* 关闭页面 */
    close(hide) {
      if (hide) {
        hide();
      }
      this.$emit("done");
    }
  }
}
</script>

<style scoped>

::v-deep .el-button+.el-button{
  margin-left: 3px;
}

</style>
